package com.ruoyi.basicsystem.controller;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.material.domain.ZnMaterial;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basicsystem.domain.ZnDepot;
import com.ruoyi.basicsystem.service.IZnDepotService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 仓库Controller
 * 
 * @author ruoyi
 * @date 2021-12-28
 */
@RestController
@RequestMapping("/basicsystem/depot")
public class ZnDepotController extends BaseController
{
    @Autowired
    private IZnDepotService znDepotService;

    /**
     * 查询仓库列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZnDepot znDepot)
    {
        startPage();
        znDepot.setDeleteFlag("0");
        znDepot.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        List<ZnDepot> list = znDepotService.selectZnDepotList(znDepot);
        return getDataTable(list);
    }
    /**
     * 查询所有仓库列表
     */
    //@PreAuthorize("@ss.hasPermi('material:depot:list')")
    @GetMapping("/listAll")
    public AjaxResult listAll(ZnDepot znDepot)
    {
        znDepot.setDeleteFlag("0");
        znDepot.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        List<ZnDepot> list = znDepotService.selectZnDepotList(znDepot);
        AjaxResult ajaxResult=new AjaxResult();
        Collections.sort(list, new Comparator<ZnDepot>() {
            @Override
            public int compare(ZnDepot o1, ZnDepot o2) {
                if(StringUtils.isEmpty(o1.getSort())){
                    return -1;
                }
                if(StringUtils.isEmpty(o2.getSort())){
                    return -1;
                }
                return o1.getSort().compareTo(o2.getSort());
            }
        });
        ajaxResult.put("list",list);
        return ajaxResult;
    }

    /**
     * 查询用户当前所有仓库列表
     */
    //@PreAuthorize("@ss.hasPermi('material:depot:list')")
    @GetMapping("/listDepotByCurrentUserAll")
    public AjaxResult listDepotByCurrentUserAll(ZnDepot znDepot)
    {
        List<ZnDepot> list = znDepotService.findDepotByCurrentUser(znDepot);
        AjaxResult ajaxResult=new AjaxResult();
        Collections.sort(list, new Comparator<ZnDepot>() {
            @Override
            public int compare(ZnDepot o1, ZnDepot o2) {
                if(StringUtils.isEmpty(o1.getSort())){
                    return -1;
                }
                if(StringUtils.isEmpty(o2.getSort())){
                    return -1;
                }
                return o1.getSort().compareTo(o2.getSort());
            }
        });
        ajaxResult.put("list",list);
        return ajaxResult;
    }
    /**
     * 导出仓库列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:export')")
    @Log(title = "仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnDepot znDepot)
    {
        List<ZnDepot> list = znDepotService.selectZnDepotList(znDepot);
        ExcelUtil<ZnDepot> util = new ExcelUtil<ZnDepot>(ZnDepot.class);
        util.exportExcel(response, list, "仓库数据");
    }

    /**
     * 获取仓库详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(znDepotService.selectZnDepotById(id));
    }

    /**
     * 新增仓库
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:add')")
    @Log(title = "仓库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnDepot znDepot)
    {

        return toAjax(znDepotService.insertZnDepot(znDepot));
    }

    /**
     * 修改仓库
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:edit')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnDepot znDepot)
    {
        return toAjax(znDepotService.updateZnDepot(znDepot));
    }

    /**
     * 删除仓库
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:remove')")
    @Log(title = "仓库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(znDepotService.deleteZnDepotByIds(ids));
    }
    /**
     * 默认仓库
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:depot:remove')")
    @Log(title = "仓库", businessType = BusinessType.UPDATE)
    @PutMapping("/handleIsDefault/{id}")
    public AjaxResult handleIsDefault(@PathVariable Long id)
    {

        return znDepotService.handleIsDefault(id);
    }

    /**
     * 查询仓库列表
     */
//    @PreAuthorize("@ss.hasAnyPermi('basicsystem:depot:list,basicsystem:depot:add')")
    @GetMapping("/selectlist")
    public AjaxResult selectlist(ZnDepot znDepot)
    {
        znDepot.setDeleteFlag("0");
        znDepot.setTenantId(SecurityUtils.getLoginUser().getUser().getTenantId());
        List<ZnDepot> list = znDepotService.selectZnDepotSelectList(znDepot);
        return AjaxResult.success(list);
    }
}
